<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" id="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">登陆密码</label>
        <div class="layui-input-block">
            <input type="text"  name="userpass"  placeholder="不修改请留空"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">昵称</label>
        <div class="layui-input-block">
            <input type="text"  name="nickname" id="nickname"  placeholder="请输入昵称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-block">
            <select name="status" id="status" class="layui-select">
                <option value="1">正常</option>
                <option value="0">禁用</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/qidian-ajax.js"></script>
<script>
    // 从url中取出要编辑的用户的编号
    let userId = undefined;
    let href = location.href;
    let hrefs = href.split("?");
    if(hrefs!=undefined && hrefs.length == 2){
        let param = hrefs[1];
        let params = param.split("&");
        if(params!=undefined){
            for(let i = 0 ;i<params.length;i++){
                let ps = params[i].split("="); // [userId,1]
                if(ps!=undefined && ps.length == 2 && ps[0]=='userId'){
                    userId = ps[1];
                }
            }
        }
    }
    // 通过ajax加载要编辑的数据  这里要同步请求。  需要修改我们的ajax方法
    qdAjax({
        async:false,// 拒接异步请求
        type:"GET",
        url:"http://localhost:8080/crm/sysuser",
        data:"op=get&userId="+userId,
        success:function(res){ //｛sysuser 对象｝
            console.log(typeof res);
            res = JSON.parse(res);
            if(res!=undefined){
                $id("username").value=res.username;
                $id("nickname").value=res.nickname;
                $id("status").value=res.status;
            }
        }
    });



    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // 准备参数
            let param = "op=edit&userId="+userId+"&username="+data.field.username+"&userpass="+data.field.userpass+"&nickname="+data.field.nickname+"&status="+data.field.status;
            // 开始请求
            qdAjax({
                type:"post",
                url:"http://localhost:8080/crm/sysuser",
                data:param,
                success:function(res){
                    if(res=='1'){
                        let index = layer.alert("编辑成功",{title:"提示"},function(){
                            // 关闭弹出层
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    }else if(res=='2'){
                        layer.alert("登录名重复");
                    }else{
                        layer.alert("编辑失败");
                    }
                }
            });
            return false;
        });

    });
</script>
</body>
</html>